package com.example.api.controller;

import com.example.api.aspect.SystemControllerLog;
import com.example.api.model.entity.Admin;
import com.example.api.model.enums.ActionTypeEnum;
import com.example.api.model.support.RespBean;
import com.example.api.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.Map;
import java.util.Objects;

@Api(tags = "个人中心")
@RestController
@RequestMapping("/admin")
public class AdminInfoController {

    @Resource
    private AdminService adminService;

    @ApiOperation("获取当前登录用户信息")
    @GetMapping("/info")
    @SystemControllerLog(description = "获取当前登录用户信息", actionType = ActionTypeEnum.QUERY)
    public Admin getAdminInfo(Principal principal) {
        if (Objects.isNull(principal)) {
            return null;
        }
        String username = principal.getName();
        return adminService.getAdminInfo(username);
    }

    @ApiOperation("更新用户密码")
    @PutMapping("/pass")
    @SystemControllerLog(description = "更新用户密码", actionType = ActionTypeEnum.UPDATE)
    public RespBean updatePassword(@RequestBody Map<String, Object> info) {
        return adminService.updatePassword(info);
    }

    @ApiOperation("更新用户头像")
    @PostMapping("/avatar")
    @SystemControllerLog(description = "更新用户头像", actionType = ActionTypeEnum.CREATE)
    public RespBean updateAvatar(MultipartFile file, Integer id, Authentication authentication) throws Exception {
        return adminService.updateAvatar(file, id, authentication);
    }

}